<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          clearable
          placeholder="请输入用户名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入任务标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptId">
        <el-select placeholder="请选择部门名称" v-model="queryParams.deptId" clearable>
          <el-option
            v-for="item in allDept"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.task_statu"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shape:press:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shape:press:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shape:press:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户名称" align="center" prop="nickName"/>
      <el-table-column label="手机号" align="center" prop="phonenumber" width="110"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span> {{ scope.row.sex === 0 ? '男' : scope.row.sex === 1 ? '女' : '未知' }} </span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName"/>
      <el-table-column label="岗位" align="center" prop="postName"/>
      <el-table-column label="任务标题" align="center" prop="title"/>
      <el-table-column label="任务描述" align="center" prop="description"/>
      <el-table-column label="完成时间" align="center" prop="accomplishTime" width="150">
        <template slot-scope="scope">
          <span>{{
              scope.row.accomplishTime == null ? '尚未完成' : parseTime(scope.row.accomplishTime, '{y}-{m}-{d} {h}:{i}')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_statu" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-folder"
            size="mini"
            @click="fileVisible(scope.row)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            @click="handleCall(scope.row)"
            v-hasPermi="['shape:press:call']"
          >打回
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder-checked"
            @click="handleDownload(scope.row)"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shape:press:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入任务标题"/>
        </el-form-item>
        <el-form-item label="任务描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="预览文件" width="60%" :visible.sync="dialogVisible" center>
      <vueOfficeDocx
        :src="fileUrl"
        v-if="fileType === 'docx'"
      />
      <vueOfficePdf
        style="height: 75vh"
        :src="fileUrl"
        v-else-if="fileType === 'pdf'"
      />
      <VueOfficeExcel
        style="height: 75vh"
        :src="fileUrl"
        v-else-if="fileType === 'xlsx'"
      />
      <vueOfficePptx
        :src="fileUrl"
        v-else-if="fileType === 'pptx'"
      />
      <div v-else>
        只预览支持 .docx .pdf .excel .pptx 文件的预览
      </div>

    </el-dialog>


  </div>
</template>

<script>
import {listPress, getPress, delPress, callPress, updatePress} from "@/api/shape/press"
import {allListDept} from "@/api/system/dept";
import {downloadFileToMinio} from "@/utils/fileutils/fileUtils";
import VueOfficePptx from "@vue-office/pptx";
import VueOfficeDocx from "@vue-office/docx";
import VueOfficePdf from "@vue-office/pdf";
import VueOfficeExcel from '@vue-office/excel'

export default {
  name: "Press",
  components: {VueOfficePdf, VueOfficeDocx, VueOfficePptx,VueOfficeExcel},
  dicts: ['task_statu'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 所有的部门
      allDept: [],
      // 总条数
      total: 0,
      // 任务情况表格数据
      pressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,// 文件路径
      fileUrl: '',
      fileType:'',
      // 对话框
      dialogVisible: false,
      // 更新时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        status: null,
        createTime: null,
        nickname: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "任务标题不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "任务描述不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getAllDept()
  },
  methods: {
    // 查询部门信息
    getAllDept() {
      allListDept().then(res => {
        this.allDept = res.data
      })
    },
    /** 查询任务情况列表 */
    getList() {
      this.loading = true
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.beginCreateTime = this.daterangeCreateTime[0]
        this.queryParams.endCreateTime = this.daterangeCreateTime[1]
      }
      listPress(this.queryParams).then(response => {
        this.pressList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        publisherId: null,
        recipientId: null,
        accomplishTime: null,
        status: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.queryParams = {}
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 打回按钮操作 */
    handleCall(row) {
      const ids = row.id
      if (row.status !== 2){
        this.$modal.msgError('你只可以打回已完成的任务')
        return
      }
      this.$modal.confirm('是否确认打回任务情况编号为"' + ids + '"的数据项？').then(function () {
        return callPress(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("打回成功")
      }).catch(() => {
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids[0]
      getPress(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改任务情况"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updatePress(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务情况编号为"' + ids + '"的数据项？').then(function () {
        return delPress(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shape/press/export', {
        ...this.queryParams
      }, `press_${new Date().getTime()}.xlsx`)
    },
    // 下载文件
    handleDownload(row){
      downloadFileToMinio(row.fileName,row.originalName)
    },
    /** 预览文件*/
    fileVisible(row) {
      this.dialogVisible = true
      let lastIndex = row.url.lastIndexOf('.');
      this.fileType = lastIndex === -1 ? '' : row.url.substring(lastIndex + 1);
      this.fileUrl = row.url
    }
  }
}
</script>
